using System;

using octalforty.Waffle.ComponentModel.Navigation;
using octalforty.Waffle.ComponentModel.Security;

namespace octalforty.Waffle.Web.Engine.Web.UI
{
    /// <summary>
    /// Provides a base class for all Web Forms in octalforty Waffle Web.
    /// </summary>
    public class Page : Spring.Web.UI.Page, INavigationServiceDependency, ISecurityServiceDependency
    {
        #region Private Member Variables
        private INavigationService navigationService;
        private ISecurityService securityService;
        #endregion

        /// <summary>
        /// Initializes a new instance of <see cref="Page"/> class.
        /// </summary>
        public Page()
        {
        }

        #region Page Members
        /// <summary>
        /// Initializes data model when the page is first loaded.
        /// </summary>
        /// <remarks>
        /// This method should be overriden by the developer
        /// in order to initialize data model for the page.
        /// </remarks>
        protected override sealed void InitializeModel()
        {
            InitializeModelEx();
        }

        /// <summary>
        /// Loads the saved data model on postback.
        /// </summary>
        /// <remarks>
        /// This method should be overriden by the developer
        /// in order to load data model for the page.
        /// </remarks>
        protected override sealed void LoadModel(object savedModel)
        {
            InitializeModelEx();
        }

        /// <summary>
        /// Raises InitializeControls event.
        /// </summary>
        /// <param name="e">Event arguments.</param>
        protected override void OnInitializeControls(EventArgs e)
        {
            base.OnInitializeControls(e);
            PerformDataBind();
        }
        #endregion

        #region Overridables
        /// <summary>
        /// Initializes the model when the page is first requested and on all subsequent postbacks.
        /// </summary>
        protected virtual void InitializeModelEx()
        {
        }

        /// <summary>
        /// Binds a data source.
        /// </summary>
        protected virtual void PerformDataBind()
        {
            DataBind();
        }
        #endregion

        #region INavigationServiceDependency Members
        /// <summary>
        /// Gets or sets a reference to the <see cref="INavigationService" />.
        /// </summary>
        public INavigationService NavigationService
        {
            get { return navigationService; }
            set { navigationService = value; }
        }
        #endregion

        #region ISecurityServiceDependency Members
        /// <summary>
        /// Gets or sets a reference to the <see cref="ISecurityService" />.
        /// </summary>
        public ISecurityService SecurityService
        {
            get { return securityService; }
            set { securityService = value; }
        }
        #endregion
    }
}
